package com.shuyun.ccms.encrypt.common;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.shuyun.crypt.CryptTools;

public class EncryptCustomerInfo {
	private static final Logger logger = LoggerFactory.getLogger(EncryptCustomerInfo.class);

	//原始数据:    customerno,full_name,mobile,phone,fullname_modified,mobile_modified,fullname_most,mobile_most,phone_most
	//加密后的数据: customerno,customerno_encrypt,full_name_encrypt, mobile_encrypt,phone_encrypt,fullname_modified_encrypt,mobile_modified_encrypt,fullname_most_encrypt,
	//          mobile_most_encrypt,phone_most_encrypt
	public static String encryptTaobaoCustomerInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[9];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("EncryptCustomerInfo -- 由于客户昵称为空，导致加密客户信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 9){
				int count = tempStr.length;
				for(int i = count; i < 9; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			// 加密 customerno
			sb.append(CryptTools.encrypt(tempStr[0], CryptTools.TYPE_NICK)).append(",");
			//加密 full_name
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 phone
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 fullname_modified
			if(tempStr[4] != null){
				sb.append(CryptTools.encrypt(tempStr[4], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile_modified
			if(tempStr[5] != null){
				sb.append(CryptTools.encrypt(tempStr[5], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 fullname_most
			if(tempStr[6] != null){
				sb.append(CryptTools.encrypt(tempStr[6], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile_most
			if(tempStr[7] != null){
				sb.append(CryptTools.encrypt(tempStr[7], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 phone_most
			if(tempStr[8] != null){
				sb.append(CryptTools.encrypt(tempStr[8], CryptTools.TYPE_PHONE));
			}
			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密淘宝客户信息报错, customerNo:"+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

    //原始数据:    pin,full_name,mobile,telephone
	//加密后的数据: customerno,customerno_encrypt,full_name_encrypt, mobile_encrypt,telephone_encrypt
	public static String encryptJdCustomerInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[4];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("由于京东客户昵称为空，导致加密客户信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 4){
				int count = tempStr.length;
				for(int i = count; i < 4; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			// 加密 customerno
			sb.append(CryptTools.encrypt(tempStr[0], CryptTools.TYPE_NICK)).append(",");
			//加密 full_name
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 telephone
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密京东客户信息报错, customerNo:"+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	//原始数据:     customerno,full_name,mobile,phone,fullname_most,mobile_most,phone_most
	//加密后的数据: customerno,customerno_encrypt,full_name_encrypt, mobile_encrypt,phone_encrypt,fullname_most_encrypt,mobile_most_encrypt,phone_most_encrypt
	public static String encryptYhdCustomerInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[7];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("由于一号店客户昵称为空，导致加密客户信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 7){
				int count = tempStr.length;
				for(int i = count; i < 7; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			// 加密 customerno
			sb.append(CryptTools.encrypt(tempStr[0], CryptTools.TYPE_NICK)).append(",");
			//加密 full_name
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 telephone
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 fullname_most
			if(tempStr[4] != null){
				sb.append(CryptTools.encrypt(tempStr[4], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile_most
			if(tempStr[5] != null){
				sb.append(CryptTools.encrypt(tempStr[5], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 phone_most
			if(tempStr[6] != null){
				sb.append(CryptTools.encrypt(tempStr[6], CryptTools.TYPE_PHONE));
			}
			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密京东客户信息报错, customerNo:"+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}
	//原始数据:     customerno,full_name,mobile,phone,fullname_most,mobile_most,phone_most
	//加密后的数据: customerno,customerno_encrypt,full_name_encrypt, mobile_encrypt,phone_encrypt,fullname_most_encrypt,mobile_most_encrypt,phone_most_encrypt
	public static String encryptDdCustomerInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[7];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("由于当当客户昵称为空，导致加密客户信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 7){
				int count = tempStr.length;
				for(int i = count; i < 7; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			// 加密 customerno
			sb.append(CryptTools.encrypt(tempStr[0], CryptTools.TYPE_NICK)).append(",");
			//加密 full_name
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 phone
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 fullname_most
			if(tempStr[4] != null){
				sb.append(CryptTools.encrypt(tempStr[4], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 mobile_most
			if(tempStr[5] != null){
				sb.append(CryptTools.encrypt(tempStr[5], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 phone_most
			if(tempStr[6] != null){
				sb.append(CryptTools.encrypt(tempStr[6], CryptTools.TYPE_PHONE));
			}
			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密当当客户信息报错, customerNo:"+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	//原始数据: customerno,customer_name,customer_name_modify,customer_phone,customer_phone_modify,invoice_recipient_name,invoice_recipient_phone,invoice_recipient_hand_phone
	//加密后的数据: customerno,customerno_encrypt,customer_name_encrypt, customer_name_modify_encrypt,customer_phone_encrypt,customer_phone_modify_encrypt, invoice_recipient_name_encrypt,invoice_recipient_phone_encrypt, invoice_recipient_hand_phone_encrypt
	public static String encryptSuningCustomerInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[8];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("由于苏宁客户昵称为空，导致加密客户信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 8){
				int count = tempStr.length;
				for(int i = count; i < 8; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			// 加密 customerno
			sb.append(CryptTools.encrypt(tempStr[0], CryptTools.TYPE_NICK)).append(",");
			//加密 customer_name
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 customer_name_modify
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 customer_phone
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 customer_phone_modify
			if(tempStr[4] != null){
				sb.append(CryptTools.encrypt(tempStr[4], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 invoice_recipient_name
			if(tempStr[5] != null){
				sb.append(CryptTools.encrypt(tempStr[5], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 invoice_recipient_phone
			if(tempStr[6] != null){
				sb.append(CryptTools.encrypt(tempStr[6], CryptTools.TYPE_PHONE)).append(",");
			}else{
				sb.append(",");
			}
			//加密 invoice_recipient_hand_phone
			if(tempStr[7] != null){
				sb.append(CryptTools.encrypt(tempStr[7], CryptTools.TYPE_PHONE));
			}
			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密苏宁客户信息报错, customerNo:"+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	public static String encryptCustomerPhone(String[] originDatas){
		StringBuffer sb = new StringBuffer();
		Set<String> phoneSet = new HashSet<String>(Arrays.asList(originDatas));
		phoneSet.stream().filter(phone-> !phone.endsWith("$1$")).forEach(phone ->{
			if(StringUtils.isNotBlank(phone)){
			//	System.out.println("phone: " + phone + "encryphone: " + CryptTools.encrypt(phone, CryptTools.TYPE_PHONE));
				sb.append(phone).append(",").append(CryptTools.encrypt(phone, CryptTools.TYPE_PHONE)).append("\n");
			}
		});
		return sb.toString();
	}


	//原始数据:     order_id, customerno, receiver_name, receiver_mobile
	//加密后的数据: order_id, customerno_encrypt, receiver_name_encrypt, receiver_mobile_encrypt
	public static String encryptOrderInfo(String[] originDatas){
	//	logger.info("EncryptCustomerInfo -- 加密客户信息开始.customerno:" + originDatas[0]);
		StringBuffer sb = new StringBuffer();
		try {
			//long start = System.currentTimeMillis();
			String[] tempStr = new String[4];
			int cnt = 0;

			if(originDatas[0] == null){
				logger.info("由于订单编号为空，导致加密订单信息中止");
				return null;
			}
			for(String str : originDatas){
				tempStr[cnt] = str;
				cnt++;
			}
			if(cnt < 4){
				int count = tempStr.length;
				for(int i = count; i < 4; i++){
					if(count == 0){
						tempStr[i] = null;
					}else{
						tempStr[i-1] = null;
					}
				}
			}
			sb.append(tempStr[0]).append(",");
			//加密 customerno
			if(tempStr[1] != null){
				sb.append(CryptTools.encrypt(tempStr[1], CryptTools.TYPE_NICK)).append(",");
			}else{
				sb.append(",");
			}
			//加密 receiver_name
			if(tempStr[2] != null){
				sb.append(CryptTools.encrypt(tempStr[2], CryptTools.TYPE_RECEIVER_NAME)).append(",");
			}else{
				sb.append(",");
			}
			//加密 receiver_mobile
			if(tempStr[3] != null){
				sb.append(CryptTools.encrypt(tempStr[3], CryptTools.TYPE_PHONE)).append(",");
			}

			//long end = System.currentTimeMillis();
			//logger.info("EncryptCustomerInfo -- 加密客户信息結束.耗时: "+(end-start) + "ms");
		} catch (Exception e) {
	        logger.error("加密订单信息报错, orderId: "+originDatas[0]);
			e.printStackTrace();
			throw e;
		}
		return sb.toString();
	}

	public static void main(String[] args){
		String[] str = new String[9];
		str[0] ="00001e56a01869e9ba88ff004f98d0da";
		str[1] ="e4933be957bb8bb05a2a82e55b9fa498";
		str[3] ="吴晨";
		str[4] ="151****0103";
		str[5] ="修冬莲";

		String[] des = new String[20];
		des = str;

		List<String> list = Lists.newArrayList(des);
		list.stream().forEach(string ->
		 	System.out.println(string)

				);

	}
}
